<template>
  <div>
    <el-dialog
      :title="obj.isadd ? '活动添加' : '活动编辑'"
      :visible.sync="obj.istrue"
      width="40%"
      @close="cancel()"
    >
      <span>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="活动名称">
            <el-input v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="活动名称">
            <el-date-picker
              v-model="arrtime"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="changetime"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="一级分类">
            <el-select
              v-model="form.first_cateid"
              placeholder="请选择"
              @change="changesecond"
            >
              <el-option
                v-for="item in catelists"
                :key="item.id"
                :label="item.catename"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="二级分类">
            <el-select
              v-model="form.second_cateid"
              placeholder="请选择"
              @change="changetry"
            >
              <el-option
                v-for="item in secondarr"
                :key="item.id"
                :label="item.catename"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品">
            <el-select v-model="form.goodsid" placeholder="请选择">
              <el-option
                v-for="item in tryarr"
                :key="item.id"
                :label="item.goodsname"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch
              v-model="form.status"
              :active-value="1"
              :inactive-value="2"
            ></el-switch>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit" v-if="obj.isadd"
          >确 定</el-button
        >
        <el-button type="primary" @click="edit" v-else>修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import {
  goodslist,
  catelist,
  seckadd,
  seckinfo,
  seckedit,
} from "../../../requset/api";
import { successAlert, warningAlert } from "../../../util/alert.js";
export default {
  props: ["obj"],
  data() {
    return {
      secondarr: [],
      tryarr: [],
      arrtime: [],
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      catelists: "cate/list",
      goodslists: "goods/list",
    }),
  },
  methods: {
    ...mapActions({
      reqChangeCate: "cate/reqChangeCate",
      reqChangeSeck: "seckill/reqChangeSeck",
    }),
    emty() {
      (this.secondarr = []),
        (this.tryarr = []),
        (this.arrtime = []),
        (this.form = {
          title: "",
          begintime: "",
          endtime: "",
          first_cateid: "",
          second_cateid: "",
          goodsid: "",
          status: 1,
        });
    },
    cancel() {
      this.obj.istrue = false;
      this.obj.isadd = true;
      this.emty();
    },
    submit() {
      this.form.begintime = new Date(this.arrtime[0]).getTime();
      this.form.endtime = new Date(this.arrtime[1]).getTime();
      seckadd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqChangeSeck();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    getone(id) {
      seckinfo({ id: id }).then((res) => {
        let data = res.data.list;
        this.form=data
        this.arrtime.push(Number(data.begintime))
        this.arrtime.push(Number(data.endtime))
      });
      catelist({ pid: this.form.first_cateid }).then((res) => {
        this.secondarr = res.data.list;
      });
      goodslist({
        fid: this.form.first_cateid,
        sid: this.form.second_cateid,
      }).then((res) => {
        this.tryarr = res.data.list;
      });
    },
    edit() {
      seckedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqChangeSeck();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    changesecond() {
      catelist({ pid: this.form.first_cateid }).then((res) => {
        this.secondarr = res.data.list;
        (this.form.second_cateid = ""), (this.form.goodsid = "");
      });
    },
    changetry() {
      goodslist({
        fid: this.form.first_cateid,
        sid: this.form.second_cateid,
      }).then((res) => {
        this.tryarr = res.data.list;
        this.form.goodsid = "";
      });
    },
    changetime() {
      console.log(this.arrtime);
    },
  },
  mounted() {
    this.reqChangeCate();
  },
};
</script>

<style lang="scss" scoped>
</style>